<template>
	<view class="CloudInteraction">
		<view :style="{'height': `${statusBarHeight}px`, 'background-color': '#060143'}"></view>
		<uni-nav-bar backgroundColor="#060143;" color="#FFF" :title="$t('language.live6')" @clickLeft="goBack" left-icon="left"
			:border="false" />
		<view class="logo">
			<image class="logo-ic" src="@/static/logo.png" alt="" />
			<view class="lang-wrap" @click="changeLanguage">
				<image src="../../static/100.png" mode=""></image>
				<text>{{$t('locale.lang')}}</text>
			</view>
		</view>
		<view class="VotingInfoHeader">
			<view class="cue-text">
				{{$t('language.live6')}}
			</view>
			<view class="title">
				{{cloudTitle}}
			</view>
			<view class="time">
				<uni-dateformat format="yyyy-MM-dd" :date="add_time * 1000"></uni-dateformat>
			</view>
		</view>

		<view class="formText">
			<!-- <input ref="numberText"  type="text" :disabled='isDisabled' placeholder="中文"/> -->
			<input ref="numberText" v-for="(item,index) in numberInput" v-model="numberInputArr[index]" :key="index"
				type="text" :disabled='isDisabled'
				:placeholder="$t('language.name') == '中文' ? infoMessage.intro : infoMessage.e_intro" />

			<view class="submitCloud" @click="submitBtn()" v-if="onTab == 1">{{$t('language.live4')}}</view>
		</view>
	</view>
</template>
<script>
	import {
		HTTP_REQUEST_URL
	} from '@/config/app';
	import uniIcons from "@/uni_modules/uni-icons/components/uni-icons/uni-icons.vue";
	import uniDateformat from "@/uni_modules/uni-dateformat/components/uni-dateformat/uni-dateformat.vue";
	import {
		cloud_info,
		cloud_save
	} from "@/api/user";
	let timer = null
	export default {
		name: 'CloudInteraction',
		components: {
			uniIcons,
			uniDateformat
		},
		data() {
			return {
				baseUrl: HTTP_REQUEST_URL,
				statusBarHeight: 44,
				onTab: '',
				textCloud: '',
				cloud_id: '',
				event_id: '',
				cloudTitle: '',
				add_time: '',
				numberInput: '',
				numberInputArr: [],
				infoMessage: '',
				isDisabled: false
			}
		},
		onLoad(o) {
			this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight
			if(o.scene) {
				let scene = o.scene.split('_')
				this.event_id = scene[0];
				this.cloud_id = scene[1];
			} else {
				let event_id = o.event_id;
				let cloud_id = o.cloud_id;
				this.event_id = event_id;
				this.cloud_id = cloud_id;
				let onTab = o.type;
				this.onTab = onTab;
			}
			this.getCloudListInfo();
		},
		created() {},
		methods: {
			async getCloudListInfo() {
				const response = await cloud_info({
					event_id: this.event_id,
					cloud_id: this.cloud_id
				})
				if (response.code == 200) {
					this.cloudTitle = response.data.title;
					this.add_time = response.data.add_time;
					this.numberInput = response.data.number;

					this.infoMessage = response.data;
					let answerList = response.data.answer;
					this.onTab = answerList.length > 0 ? 2 : 1
					if (answerList.length != 0) {
						this.isDisabled = true;
						this.$nextTick(() => {
							for (let i = 0; i < answerList.length; i++) {
								this.numberInputArr.push(answerList[i])
							}
						})
					} else {
						for (let i = 0; i < response.data.number; i++) {
							this.numberInputArr.push('')
						}
					}
				} else {
					console.log(response.msg);
				}

			},
			submitBtn() {
				const t = this
				let arrayText = this.numberInputArr
				let text = ''
				for (let i = 0; i < arrayText.length; i++) {
					if (arrayText[i] == '') {
						let messagetips = t.$i18n.locale == 'zh' ? '请输入所有关键词' : 'Please enter all keywords'
						uni.showToast({
							title: messagetips,
							icon: 'none'
						})
						return false
					} else {
						text = text + arrayText[i] + '|'
					}
				}
				console.log(text)
				cloud_save({
					event_id: t.event_id,
					cloud_id: t.cloud_id,
					words: text
				}).then(
					res => {
						if (res.code == '200') {
							console.log(res)
							t.onTab = 2;
							let messagetips = t.$i18n.locale == 'zh' ? '提交成功' : 'Submit successfully'
							uni.showToast({
								title: messagetips,
								icon: 'success',
								success() {
									uni.redirectTo({
										url: '/pages/LiveInteraction/CloudList?evenId=' + t.event_id
									})
								}
							})
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					},
					err => {
						console.log(err)
					}
				);
			},
			goBack() {
				uni.redirectTo({
					url: '/pages/LiveInteraction/CloudList?evenId=' + this.event_id
				})
				// uni.navigateBack()
			},
			changeLanguage() {
				if (this.$i18n.locale == 'zh') {
					this.$i18n.locale = 'en';
					uni.setStorageSync('locale', 'en')
				} else {
					this.$i18n.locale = 'zh';
					uni.setStorageSync('locale', 'zh')
				}
			}
		}
	}
</script>
<style lang="scss">

	.logo {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20upx 30upx;
		background-color: #060143;

		.logo-ic {
			width: 78upx;
			height: 64upx;
		}

		.lang-wrap {
			display: flex;
			align-items: center;
			color: #fff;
			font-size: 24upx;

			image {
				width: 30upx;
				height: 30upx;
				margin-right: 12upx;
			}
		}
	}

	page {
		background: #050033;
	}

	.CloudInteraction {
		width: 100%;
		min-height: 100vh;
		background: #050033;
		box-sizing: border-box;
	}

	.headerChangeLanguage {
		height: 100upx;
		background: #fff;
		padding: 0 30upx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-shadow: 0 2upx 4upx #ccc;
		width: 100%;
		box-sizing: border-box;
	}

	.logo image {
		width: 178upx;
		height: 30upx;
	}


	.language {
		flex-shrink: 0;
	}

	.langeageZh {
		height: 30upx;
		width: 86upx;
		background: url($image-base-url + '/static/img/EN-17.png') no-repeat;
		background-size: 86upx 30upx;
	}

	.langeageEn {
		height: 30upx;
		width: 86upx;
		background: url($image-base-url + '/static/img/CH-17.png') no-repeat;
		background-size: 86upx 30upx;
	}

	.VotingInfoHeader {
		padding: 30upx;
	}

	.cue-text {
		display: inline-block;
		width: auto;
		height: 44upx;
		line-height: 44upx;
		text-align: center;
		padding: 0 24upx;
		border-radius: 60upx 60upx 60upx 0;
		background-color: #d3a86a;
		color: #fff;
		font-size: 24upx;
	}

	.title {
		// min-height: 92upx;
		padding-top: 20upx;
		padding-bottom: 10upx;
		color: #fff;
		font-size: 32upx;
		// border-bottom: 2upx solid #ebebeb;
	}

	.time {
		color: #959596;
		font-size: 24upx;
		display: flex;
	}

	.formText {
		padding: 30upx;
	}

	.formText input {
		background: #fff;
		border: none;
		resize: none;
		padding: 20upx 30upx;
		font-size: 30upx;
		border-radius: 8upx;
		margin-bottom: 30upx;
		color: #000000;
	}

	.submitCloud {
		width: 100%;
		text-align: center;
		height: 80upx;
		line-height: 80upx;
		border-radius: 80upx;
		margin-top: 100upx;
		background: #b88d5b;
		color: #fff;
	}
</style>